package com.zzyl.nursing.controller.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.AlertRule;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.dto.UserRequestLoginDto;
import com.zzyl.nursing.dto.QueryDevicePropertyStatusDto;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.service.IWeChatService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.DeviceData;
import org.springframework.data.redis.core.RedisTemplate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import com.zzyl.nursing.vo.FamilyMemberPageVo;
import com.zzyl.nursing.vo.ServicePropertyVo;
import com.zzyl.nursing.vo.UserRequestLoginVo;
import com.zzyl.nursing.vo.MyElderVo;
import com.zzyl.nursing.vo.PropertyStatusListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2025-07-18
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(FamilyMemberController.class);
    
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private IWeChatService weChatService;

    @Autowired
    private IFamilyMemberElderService familyMemberElderService;
    
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录
     * @param userRequestLoginDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public R login(@ApiParam("登录参数") @RequestBody UserRequestLoginDto userRequestLoginDto) {
        UserRequestLoginVo userRequestLoginVo = weChatService.login(userRequestLoginDto);
        return R.ok(userRequestLoginVo);
    }

    @GetMapping("/my")
    @ApiOperation("查询家人列表")
    public AjaxResult my(){
        List<MyElderVo> list = familyMemberElderService.selectMyElderList();
        return success(list);
    }



    @GetMapping("/list-by-page")
    @ApiOperation("分页查询家人列表")
    public AjaxResult listByPage(
            @ApiParam(value = "页码", required = false) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", required = false) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        startPage();
        List<FamilyMemberPageVo> list = familyMemberElderService.selectFamilyMemberListByPage();
        return success(list);
    }
    @PostMapping("/add")
    @ApiOperation("绑定家人")
    public AjaxResult add(@ApiParam("绑定家属参数") @RequestBody FamilyMemberElderDto dto){
        familyMemberElderService.bindElder(dto);
        return success();
    }

    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询健康数据")
    public R<List<ServicePropertyVo>> queryServiceProperties(
            @ApiParam(value = "设备ID", required = true) @PathVariable("iotId") String iotId)
    {
        List<ServicePropertyVo> result = familyMemberElderService.queryServiceProperties(iotId);
        return R.ok(result);
    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation("查询健康数据（旧版）")
    public R<PropertyStatusListVo> queryDevicePropertyStatus(
            @ApiParam(value = "查询参数", required = true) @RequestBody QueryDevicePropertyStatusDto dto)
    {
        PropertyStatusListVo result = familyMemberElderService.queryDevicePropertyStatus(dto);
        return R.ok(result);
    }

    /**
     * 删除ById
     * @param id
     * @return
     */
    @ApiOperation("删除ById")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@ApiParam(value = "id", required = true)  Long id) {
        familyMemberElderService.deleteById(id);
        return AjaxResult.success();
    }
}
